package com.naishuo.bigevent.service.impl;

import com.naishuo.bigevent.mapper.ArticleMapper;
import com.naishuo.bigevent.pojo.Article;
import com.naishuo.bigevent.pojo.PageBean;
import com.naishuo.bigevent.service.ArticleService;
import com.naishuo.bigevent.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceimpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public void addarticle(Article article) {

        Map<String,Object> map= ThreadLocalUtil.get();
        Integer id=(Integer) map.get("id");
        article.setCreateUser(id);
        articleMapper.addarticle(article);
    }

    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {

        System.out.println(pageNum+" "+pageSize+" "+categoryId+" "+state);
        PageBean<Article> pageBean=new PageBean<>();
        pageBean.setItems(articleMapper.list(pageNum,pageSize,categoryId,state));
        Long total= (long) articleMapper.list(pageNum,pageSize,categoryId,state).size();
        pageBean.setTotal(total);
        System.out.println(pageBean);
        return pageBean;
    }

    @Override
    public List<Article> findAllById(Integer id) {

        return articleMapper.findAllById(id);
    }

    @Override
    public void updatearticle(Article article) {

        System.out.println(article);
        Map<String,Object> map= ThreadLocalUtil.get();
        Integer id=(Integer) map.get("id");
        article.setCreateUser(id);
        articleMapper.updatearticle(article);
        System.out.println(article);
    }

    @Override
    public void deleteArticle(Integer id) {

        articleMapper.deleteArticle(id);
    }
}
